package com.lengxf.redis.service;

import lombok.AllArgsConstructor;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@AllArgsConstructor
@SuppressWarnings({"rawtypes", "unchecked"})
public class RedisBitMapService {

    private final RedisTemplate redisTemplate;

    public void set(String key, Long offset) {
        // setBit 返回的是原来的值 ;而不是是否执行成功
        redisTemplate.opsForValue().setBit(key, offset, true);
    }

    public Boolean get(String key, Long offset) {
        return redisTemplate.opsForValue().getBit(key, offset);
    }

    public Long bitCount(String key) {
        return (Long) redisTemplate.execute((RedisCallback<Long>) redisConnection -> redisConnection.bitCount(key.getBytes()));
    }


}
